package com.xingfly.repository;

import com.xingfly.model.Interest;
import com.xingfly.model.Tag;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by SuperS on 2016/12/7.
 */
public interface InterestRepository extends JpaRepository<Interest, Long> {
    //删除interest根据用户ID和tagID
    @Transactional
    @Modifying
    @Query("DELETE FROM Interest i WHERE i.user.id = :userId AND i.tag.id=:tagId")
    void deleteByUserIdAndTagId(@Param("userId") Long userId, @Param("tagId") Long tagId);


    @Query("SELECT i.user.id FROM Interest i WHERE i.tag.id=:tagId")
    List<Long> findUsersIdByTagId(@Param("tagId") Long tagId);

    Interest findByUserIdAndTagId(Long userId, Long tagId);

    @Query("SELECT i.tag FROM Interest i WHERE i.user.id=:userId")
    List<Tag> findTagsByUserId(@Param("userId") Long userId);
}
